﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Intranet.Controllers
{
    public class ClientsController : IntranetController
    {
        //
        // GET: /Clients/

        public ActionResult Index()
        {
            using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
            {
                return View(entities.clients.Where(p => p.isApproved).ToList());
            }
        }

        public ActionResult ToBeApproved()
        {
            using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
            {
                return View(entities.clients.Where(p => !p.isApproved).ToList());
            }
        }

        //
        // GET: /Clients/Details/5

        public ActionResult Details(long id)
        {
            using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
            {
                if (entities.clients.Any(p => p.id == id))
                {
                    return View(entities.clients.First(p => p.id == id));
                }
                else
                {
                    return RedirectToAction("Error", new { message = "Unable to display the client as the specified client cannot be found" });
                }
            }
        }

        //
        // GET: /Clients/Create
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Clients/Create

        [HttpPost]
        public ActionResult Create(Models.client client)
        {
            try
            {
                // TODO: Add insert logic here
                using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
                {
                    if (entities.clients.Any())
                    {
                        int lastCode = entities.clients.Max(p => p.clientCode);
                        lastCode++;
                        client.clientCode = lastCode;
                    }
                    else
                    {
                        client.clientCode = 1;
                    }
                    entities.clients.AddObject(client);
                    entities.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Clients/Edit/5
        [HttpGet]
        public ActionResult Edit(long id)
        {
            using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
            {
                if (entities.clients.Any(p => p.id == id))
                {
                    Models.client c = entities.clients.First(p => p.id == id);
                    if (c.isApproved)
                    {
                        return RedirectToAction("Error", new { message = "The specified client has been approved and therefore cannot be amended." });
                    }
                    else
                    {
                        return View(c);
                    }
                }
                else
                {
                    return RedirectToAction("Error", new { message = "Unable to edit the client as the specified client cannot be found" });
                }
            }
        }

        //
        // POST: /Clients/Edit/5

        [HttpPost]
        public ActionResult Edit(long id, Models.client client)
        {
            try
            {
                // TODO: Add update logic here
                using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
                {
                    if (entities.clients.Any(p => p.id == id))
                    {
                        Models.client oldClient = entities.clients.First(p => p.id == id);
                        if (!oldClient.isApproved)
                        {
                            oldClient.clientName = client.clientName;
                            entities.SaveChanges();    
                        }
                        
                    }
                    else
                    {
                        return RedirectToAction("Error", new { message = "Unable to edit the client as the specified client cannot be found" });
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Clients/Approve/5
        [HttpGet]
        [Authorize(Roles="ClientApprovers")]
        public ActionResult Approve(long id)
        {
            using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
            {
                if (entities.clients.Any(p => p.id == id))
                {
                    return View(entities.clients.First(p => p.id == id));
                }
                else
                {
                    return RedirectToAction("Error", new { message = "Unable to edit the client as the specified client cannot be found" });
                }
            }
        }

        //
        // POST: /Clients/Approve/5

        [Authorize(Roles = "ClientApprovers")]
        [HttpPost]
        public ActionResult Approve(long id, Models.client client)
        {
            try
            {
                // TODO: Add update logic here
                using (Models.intranetDBEntities entities = new Models.intranetDBEntities())
                {
                    if (entities.clients.Any(p => p.id == id))
                    {
                        Models.client oldClient = entities.clients.First(p => p.id == id);
                        oldClient.isApproved = client.isApproved;
                        entities.SaveChanges();
                    }
                    else
                    {
                        return RedirectToAction("Error", new { message = "Unable to edit the client as the specified client cannot be found" });
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Clients/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Clients/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
